package main

import (
	log "github.com/sirupsen/logrus"
	cmd2 "litter-api/cmd"
	"litter-api/ctl"
)

/*func init() {
	//环境配置
	//conf.InitEnv()
	//初始化 配置文件
	//conf.InitYaml()
}*/

var err error

func main() {
	//初始化数据库
	//InitDb()
	//开启服务
	//InitHttpServe()
	defer func() {
		if ctl.GracefullyStop {
			log.WithFields(log.Fields{"location": "main", "method": "main"}).Info("Gracefully shutdown.")
		}
	}()
	go func() {
		err = ctl.HandleSignals()
		if err != nil {
			log.WithFields(log.Fields{"location": "main", "method": "main"}).Fatal(err)
		}
	}()
	err := cmd2.Execute()
	if err != nil {
		log.WithFields(log.Fields{"location": "main", "method": "main"}).Fatal(err)
	}
}

/*func InitHttpServe() {
	//gin.SetMode(gin.ReleaseMode)
	if conf.GetEnv().Name == "pro" || conf.GetEnv().Name == "testing" {
		gin.SetMode(gin.ReleaseMode)
	}

	r := gin.Default()
	engine := router.NewRouter(r)

	//开启中间件 允许使用跨域请求
	//engine.Engine.Use(middleware.Cors())

	// 性能测试
	//pprof.Register(r)
	//设置路由
	router.Route(engine)

	srv := &http.Server{
		Addr:    fmt.Sprintf(":%s", conf.GetEnv().ServerPort),
		Handler: r,
	}

	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()
	log.WithFields(log.Fields{
		"Server Port:": conf.GetEnv().ServerPort,
	}).Info("api: Running HTTP server")

	//平滑重启
	boot := bootstrap.New(srv)
	boot.InitSignal()

}

func InitDb() {
	pool.InitLog()

	// 初始化 DB
	err := pool.InitMysql()
	if err != nil {
		log.Fatal("mysql init err", zap.Error(err))
	}
	// 初始化 Redis
	//_, err = pool.NewCache()
	//if err != nil {
	//	log.Fatal("redis init err", zap.Error(err))
	//}
}*/
